<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Controller
 *
 * @author Anto
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Controller extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     *	- or -
     * 		http://example.com/index.php/welcome/index
     *	- or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
    public function view($page = 'home') {
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php')) {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
    }

    public function crop_image() {
        $this->load->model('model');
        $data['title'] = 'Crop Image';
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('x', 'x', 'required');
        $this->form_validation->set_rules('y', 'y', 'required');
        $this->form_validation->set_rules('width', 'width', 'required');
        $this->form_validation->set_rules('height', 'height', 'required');
        
        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('news/cropImage', $data);
            $this->load->view('templates/footer');

        } else {
            $this->model->set_crop();
            $this->load->view('news/success');
        }

        
    }
    
    public function login() {
        $this->load->model('model');
        $data['title'] = 'Login';
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('x', 'x', 'required');
        $this->form_validation->set_rules('y', 'y', 'required');
        
        if ($this->form_validation->run() === FALSE) {
            
            $this->load->view('templates/header', $data);
            $this->load->view('pages/login');
            $this->load->view('templates/footer');

        } else {
            $this->model->check_user();
            $this->load->view('news/success');
        }
    }
    
    public function login2() {
        $this->load->model('model');
        $data['title'] = 'Login';
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('user_name', 'user_name', 'required');
        $this->form_validation->set_rules('pass', 'pass', 'required');
        
        if ($this->form_validation->run() === FALSE) {
            $this->load->view('templates/header', $data);
            $this->load->view('pages/login', $data);
            $this->load->view('templates/footer');

        } 
        
        else {
            $this->model->check_user();
            $this->load->view('news/success');
        }
    }
    
    public function check_login() {
        $this->load->model('model');
        $user_name = $this->input->post('user_name');
        $data['user'] = $this->model->check_user($user_name);
        if (empty($data['user'])) {
                echo false;
        }
        else echo true;
    }
}
